package cn.js.sangu.unitStore.utils;

import cn.hutool.core.util.StrUtil;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 自定义工具类
 */
public class LinsUtil {


    //关键字捕捉和替换
    public static String keywordReplace(String content,String keyword,String newKeyword){
        if (StrUtil.isNotBlank(content)){
            if (StrUtil.isNotBlank(keyword)){
                if (StrUtil.isNotBlank(newKeyword)){
                    return content.replace(keyword, newKeyword);
                }else {
                    return content.replace(keyword,"***");
                }
            }else {
                //关键字未定义
                return content.replace("傻子","可爱");
            }
        }else {
            return content;
        }
    }

    //计算文件大小
    public static String computeFileSize(Long IntSize){
        String res;
        DecimalFormat df = new DecimalFormat("0.00");
        if (IntSize < 1024) {
            res = df.format((double) IntSize) + "字节";
        } else if (IntSize < 1048576) {
            res = df.format((double) IntSize / 1024) + "KB";
        } else if (IntSize < 1073741824) {
            res = df.format((double) IntSize / 1048576) + "MB";
        } else {
            res = df.format((double) IntSize / 1073741824) + "GB";
        }
        return res;
    }

    /**
     * 补充作者和修改人名称
     */
//    private <T> List<T> suppleName(List<T> list){
//        for (Doc doc : list) {
//            //作者
//            User author = userService.getById(doc.getAuthor());
//            if (null != author) doc.setAuthorName(author.getName());
//            //修改人
//            User modifier = userService.getById(doc.getModifier());
//            if (null != modifier) doc.setModifierName(modifier.getName());
//        }
////        list.forEach(doc -> doc.setModifierName(userService.getById(doc.getModifier()).getName()));
//        return list;
//    }

    /**
     * 获取最近N天的日期
     */
    public static List<String> recentDates(Integer n,String format){
        LocalDate today = LocalDate.now();
        List<String> recentDates = new ArrayList<>();
        for (int i = 6; i >= 0; i--) {
            //当前日期向前推i天
            LocalDate date = today.minusDays(i);
            recentDates.add(date.format(DateTimeFormatter.ofPattern(format)));
        }
        return recentDates;
    }

}
